<template>
  <div class="record">
    <tab>
      <tab-item selected @on-item-click="change(0)">{{$t('my purchase')}}</tab-item>
      <tab-item  @on-item-click="change(1)">{{$t('my sale')}}</tab-item>
    </tab>
    <!--<div class="drop-down">-->
        <!--<select name="" id="select" @change="find($event)">-->
            <!--<option value="" >{{$t('total')}}</option>-->
            <!--<option value="" >{{$t('finish')}}</option>-->
            <!--<option value="" >{{$t('trading')}}</option>-->
            <!--<option value="" >{{$t('untrading')}}</option>-->
            <!--<option value="" >{{$t('cancel')}}</option>-->
        <!--</select>-->
    <!--</div>-->
    <!--<div class="wire"></div>-->
    <div class="wrapper">
      <scroller
        :on-refresh="refresh"
        :on-infinite="infinite"
        :noDataText="$t('no more')"
        ref="myScroller"
      >
        <div class="content mairu" >
          <div class="list" v-for="i in data">
            <ul>
              <li @click="goNext(i.deal_sn,i.status, type)">
                <div class="left">
                  <p class="p2">
                    {{type === 0 ? $t('want by') : $t('want sell')}}
                    <!--:{{i.user_id}}-->
                  </p>
                  <p class="p4">{{$t('num')}}：{{i.cny}}</p>
                </div>
                <div class="right">
                  <p class="p4" :style="{color: i.color}">{{$t('tradelist.finished')}}</p>
                  <p class="p3">{{i.create_time}}</p>
                </div>
              </li>
              <div class="btn" v-if="i.status === '未交易'"><button @click="cancleTrade(i.deal_sn)">{{$t('cancel btn')}}</button></div>
            </ul>
          </div>
          <no-data :nomore="false" v-if="showNodata"></no-data>
        </div>
        <!--<div class="content maichu" v-if="showTab === 2">-->
      <!--<div class="list" v-for="i in data">-->
        <!--<ul>-->
          <!--<li @click="goNext(i.deal_sn,i.status, 2)">-->
            <!--<div class="left">-->
              <!--<p class="p2">{{$t('buyer')}}:{{i.user_id}}</p>-->
              <!--<p class="p4">{{$t('num')}}：{{i.cny}}</p>-->
            <!--</div>-->
            <!--<div class="right">-->
              <!--<p class="p4" :style="{color: i.color}">{{i.status}}</p>-->
              <!--<p class="p3">{{i.create_time}}</p>-->
            <!--</div>-->
          <!--</li>-->
          <!--<div class="btn" v-if="i.status === '未交易'"><button @click="cancleTrade(i.deal_sn)">{{$t('cancel btn')}}</button></div>-->
        <!--</ul>-->
      <!--</div>-->
      <!--<no-data v-if="data.length === 0"></no-data>-->
    <!--</div>-->
      </scroller>
    </div>
    </div>
</template>

<i18n>
  want by:
    en: Want To Buy
    zh-CN: 我要买
  want sell:
    en: Want To Sell
    zh-CN: 我要卖
  cancel btn:
    en: Cancle Trade
    zh-CN: 撤单
  my purchase:
    en: My Purchase
    zh-CN: 购买
  my sale:
    en: My Sale
    zh-CN: 出售
  buyer:
    en: BuyerID
    zh-CN: 买家ID
  seller:
    en: SellerID
    zh-CN: 卖家ID
  num:
    en: Number
    zh-CN: 数量
  no:
    en: No.
    zh-CN: 单号
  total:
    en: Total
    zh-CN: 全部
  finish:
    en: Finished
    zh-CN: 已结束
  trading:
    en: Trading
    zh-CN: 进行中
  untrading:
    en: Untrading
    zh-CN: 未交易
  cancel:
    en: Cancel
    zh-CN: 取消交易
  content:
    en: Do You Want To Cancle?
    zh-CN: 确认要取消？
  tradelist.trading:
    en: Trading
    zh-CN: 交易中
  tradelist.untraded:
    en: Untraded
    zh-CN: 未交易
  tradelist.Cancel:
    en: Cancel
    zh-CN: 取消交易
  tradelist.finished:
    en: Finished
    zh-CN: 交易完成
</i18n>

<script>
  import {Cell, Group, Tab, TabItem} from 'vux'
  import noData from '../../components/nodata'
  export default {
    name: 'trad',
    components: {
      Cell,
      Group,
      Tab,
      TabItem,
      noData
    },
    data () {
      return {
        data: [],
        showBtn: false,
        showTab: '',
        type: 0,
        listRows: 10,
        page: 1,
        status: 3,
        scrollLoading: true,
        showNodata: false
      }
    },
    computed: {
      // type () {
      //   console.log(this.$route.query.type)
      //   this.$route.query.type ? this.showTab = this.$route.query.type : this.showTab = 1
      //   return this.$route.query.type ? this.$route.query.type : 1
      // }
    },
    created () {
      this.init()
    },
    methods: {
      init () {
        this.change(0)
      },
      onItemClick (index) {
        console.log(index)
      },
      refresh (done) {
        this.page = 1
        this.data = []
        this.getInfo(done)
      },
      infinite (done) {
        if (!this.scrollLoading) {
          this.getInfo(done)
        } else {
          this.$refs.myScroller.finishInfinite(true)
        }
      },
      change (num) {
        this.data = []
        this.showTab = num
        this.type = num
        this.page = 1
        this.getInfo()
      },
      getInfo (done) {
        let vm = this
        this.scrollLoading = true
        let showLoding
        vm.page === 1 ? showLoding = true : showLoding = false
        this.post({
          url: `/api/logging/mydeallog`,
          showloading: showLoding,
          params: {
            token: localStorage.token,
            type: vm.type,
            listRows: vm.listRows,
            page: vm.page,
            status: vm.status
          },
          success: (data) => {
            console.log(data)
            if (data.code === 0) {
              this.$vux.toast.show({
                text: `${data.msg}`,
                type: 'text'
              })
              vm.$refs.myScroller.finishInfinite(true)
              done()
            } else if (data.code === 1) {
              let newData = data.data.data
              data.data.data.length === 0 ? vm.showNodata = true : vm.showNodata = false
              console.log(newData)
              if (newData.length < vm.listRows) {
                vm.scrollLoading = true
                // vm.data = vm.data.concat(data.data.data)
                vm.$refs.myScroller.finishInfinite(true)
              } else {
                vm.scrollLoading = false
                vm.page = vm.page + 1
              }
              let midData = newData.map(function (val, index) {
                if (val.status === '交易中') {
                  val.color = '#57a94f'
                } else if (val.status === '未交易') {
                  val.color = '#fbb752'
                }
                val.end = false
                return val
              })
              for (let i = 0; i < midData.length; i++) {
                this.data.push(midData[i])
              }
              console.log(this.data)
              done()
            }
          }
        })
      },
      find (e) {
        this.status = 4 - e.target.options.selectedIndex
        console.log(this.status)
        // this.getInfo(this.type, this.listRows, this.page,this.status)
        this.change(this.type)
      },
      goNext (id, status, type) {
        console.log(type)
        // if (status === '已完成') {
        //   this.$router.push({path: '/tradepaycoin', query: {num: id, type: type}})
        // }
        if (status === '2') {
          this.$router.push({path: '/tradepaycoin', query: {num: id, type: type}})
        }
      },
      cancleTrade (num) {
        let vue = this
        this.$vux.confirm.show({
          title: this.$t('info'),
          content: this.$t('content'),
          onConfirm () {
            console.log(localStorage.token)
            console.log(num)
            vue.post({
              url: `/api/usercenter/cancelDeal`,
              params: {
                token: localStorage.token,
                deal_sn: num
              },
              success: function (data) {
                if (data.code === 1) {
                  vue.$vux.toast.show({
                    text: `撤销成功`
                  })
                  vue.data.forEach(function (val, index) {
                    if (val.deal_sn === num) {
                      val.status = '已取消'
                      val.color = ''
                    }
                    return val
                  })
                //  vue.getInfo()
                } else if (data.code === 0) {
                  vue.$vux.toast.show({
                    text: `data.data.msg`
                  })
                }
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>
  .record{
    font-size: 0.3rem;
    height: 100%;
  }
  .wrapper{
    position: relative;
    height: calc(100% - 44px) ;
  }
  .record h5{
    height: 0.9rem;
    line-height: 0.9rem;
    margin: 0;
    font-weight: normal;
    background-color: #537fbf;
    color: #fff;
    display: flex;
  }
  .record h5 a{
    font-size: 0.4rem;
    text-decoration: none;
    color: #fff;
    flex-grow: 1;
    margin-left: 0.4rem;
  }
  .record h5 span{
    flex-grow:2 ;
    padding-left: 1rem;
  }
  .nav-tab{
    display: flex;
    list-style: none;
    height: 1rem!important;
    margin: 0;
    line-height: 1rem;

  }
  .nav-tab li{
    flex-grow: 1;
    text-align: center;
  }
  .nav-tab li div{
    width: 1.8rem;
    margin: 0 auto;
  }

  .nav-tab li a{
    color: #666;
  }
  .nav-tab li a:hover{
    color: #7294c8;
  }
  .nav-tab li div:hover{
    border-bottom: 2px solid #7294c8;
  }
  .wire{
    height: 0.1rem;
    background-color: #fafafa;
  }
  .list{
    /*border-bottom: 1px solid red;*/
  }
  .list ul{
    border-bottom: 0.01rem solid #eeeeee;
  }
  .list .ul3{
    border-bottom:none;
  }
  .list li{
    display: flex;
  }
  .list .left{
    flex-grow: 1;
    padding-left: 0.4rem;
  }
  .list .right{
    flex-grow: 1;
    width: 2.5rem;
    text-align: right;
    padding-right: 0.4rem;
  }
  p{
    line-height: .8rem;
  }
  .list .p3{
    font-size: 0.2rem;
  }
  .list .p3,.p4{
    color: #999;
  }
  .list .p5{
    color: #fbb752;
  }
  .list .p6{
    color: #57a94f;
  }
  .btn{
    width: 6.7rem;
    height: 0.9rem;
    margin: 0 auto 0.8rem;

  }
  button{
    width: 6.7rem;
    height: 0.9rem;
    background-color: #537fbf;
    font-size: 0.28rem;
    color: #fff;
    margin-top: 0.39rem;
    border: 0;
    border-radius: 4px;
  }
  .drop-down{
    height: .8rem;
    background-color: #fafafa;
  }
  .drop-down select{
    appearance: none;
    outline: none;
    width: 1.85rem;
    height: .8rem;
    display: inline-block;
    color: #666666;
    background-color:#fafafa;
    margin-right: 0.5rem;
    padding-left: 0.3rem;
    border: none;
    font-size: 0.28rem;
    float: right;
    background: url("../../assets/img/down.png") no-repeat scroll right center transparent;
  }
</style>
